﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using SIMA.Core.Data.Access;

namespace SIMA.Mvvm.Base
{
    public class ViewModelBase:INotifyPropertyChanged,IDisposable
    {
        #region Cosnts

        public const string CADCONNECTIONSTRING = "Data Source=.;Initial Catalog=SIMA;Integrated Security=True";

        #endregion

        #region Fields

        private readonly Dictionary<string, PropertyChangedEventArgs> eventArgsCache;
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Constructor

        protected ViewModelBase()
        {
            eventArgsCache = new Dictionary<string, PropertyChangedEventArgs>();
            //DataAccess.SetConnection(SIMA.Core.Enums.AccessType.FromString, CADCONNECTIONSTRING);
        }

        #endregion

        #region Methods

        #region Public Methods

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        #region Protected Methods

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs args;
            if (!eventArgsCache.TryGetValue(propertyName, out args))
            {
                args = new PropertyChangedEventArgs(propertyName);
                eventArgsCache.Add(propertyName, args);
            }
            OnPropertyChanged(args);
        }

        protected void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, args);
        }

        #endregion       

        #endregion
        
        #region Destructor

        ~ViewModelBase()
        {
            this.Dispose();
        }

        #endregion
    }
}
